import { getGoodsListReq, deleteGoodsReq, addGoodsReq } from '../request/goods'
import { getGoodsCateListReq } from '../request/goods-cate'
const actions = {
  // 获取商品列表数据
  async getGoodsList(context, params) {
    const goodsListData = await getGoodsListReq(params)
    goodsListData.goods.forEach(
      item => (item.time = new Date(item.add_time).toLocaleString())
    )
    context.commit('GET_GOODS_LIST', goodsListData)
  },
  // 删除商品
  async deleteGoods(context, id) {
    try {
      const deleteGoodsData = await deleteGoodsReq(id)
      return deleteGoodsData
    } catch (error) {
      return Promise.reject(error)
    }
  },
  // 获取商品分类数据
  async getGoodsCateList(context) {
    const goodsCateList = await getGoodsCateListReq()
    context.commit('GET_GOODS_CATE_LIST', goodsCateList)
  },
  // 添加商品
  async addGoods(context, params) {
    try {
      const addGoodsData = await addGoodsReq(params)
      console.log(addGoodsData)
    } catch (error) {
      return Promise.reject(error)
    }
  },
}
const mutations = {
  GET_GOODS_LIST(state, goodsListData) {
    state.goodsListData = goodsListData
  },
  GET_GOODS_CATE_LIST(state, goodsCateList) {
    state.goodsCateList = goodsCateList
  },
}
const state = {
  goodsListData: {}, // 商品列表对象
  goodsCateList: [], // 商品分类
}
const getters = {
  goodsList() {
    return state.goodsListData.goods
  },
}

export default {
  namespaced: true,
  actions,
  mutations,
  getters,
  state,
}
